package cn.com.anypay.manager.dto.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 微信认证配置类
 * 用于管理微信开放平台应用的配置信息
 */
@Data
@Component
@ConfigurationProperties(prefix = "wechat.auth")
public class WeChatAuthConfig {
    
    /**
     * 微信应用ID
     */
    private String appId;
    
    /**
     * 微信应用密钥
     */
    private String appSecret;
    
    /**
     * 授权回调地址
     */
    private String redirectUri;
    
    /**
     * 授权作用域，默认为snsapi_login（网站应用微信登录）
     */
    private String scope = "snsapi_login";
    
    /**
     * API调用超时时间（毫秒），默认10秒
     */
    private Integer timeout = 10000;
    
    /**
     * API调用重试次数，默认3次
     */
    private Integer retryCount = 3;
    
    /**
     * 是否启用微信认证，默认启用
     */
    private Boolean enabled = true;
    
    /**
     * 微信开放平台授权URL
     */
    private String authUrl = "https://open.weixin.qq.com/connect/qrconnect";
    
    /**
     * 微信开放平台获取访问令牌URL
     */
    private String tokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
    
    /**
     * 微信开放平台获取用户信息URL
     */
    private String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo";
    
    /**
     * 微信开放平台刷新令牌URL
     */
    private String refreshTokenUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token";
    
    /**
     * 状态参数有效期（秒），默认10分钟
     */
    private Integer stateExpireSeconds = 600;
    
    /**
     * 检查配置是否完整
     */
    public boolean isConfigured() {
        return appId != null && !appId.trim().isEmpty() &&
               appSecret != null && !appSecret.trim().isEmpty() &&
               redirectUri != null && !redirectUri.trim().isEmpty() &&
               enabled != null && enabled;
    }
    
    /**
     * 获取完整的授权URL
     */
    public String buildAuthUrl(String state) {
        if (!isConfigured()) {
            throw new IllegalStateException("微信认证配置不完整");
        }
        
        return String.format("%s?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect",
                authUrl, appId, redirectUri, scope, state);
    }
    
    /**
     * 验证配置参数
     */
    public void validate() {
        if (appId == null || appId.trim().isEmpty()) {
            throw new IllegalArgumentException("微信应用ID不能为空");
        }
        if (appSecret == null || appSecret.trim().isEmpty()) {
            throw new IllegalArgumentException("微信应用密钥不能为空");
        }
        if (redirectUri == null || redirectUri.trim().isEmpty()) {
            throw new IllegalArgumentException("微信授权回调地址不能为空");
        }
        if (timeout == null || timeout <= 0) {
            throw new IllegalArgumentException("API调用超时时间必须大于0");
        }
        if (retryCount == null || retryCount < 0) {
            throw new IllegalArgumentException("API调用重试次数不能小于0");
        }
        if (stateExpireSeconds == null || stateExpireSeconds <= 0) {
            throw new IllegalArgumentException("状态参数有效期必须大于0");
        }
    }
}